﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.Common.Dic;
using Lenovo.CFI.WebControls;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.Common;
using Lenovo.CFI.BLL.Sys;
using Lenovo.CFI.BLL.DicMgr;

namespace Lenovo.CFI.Web.VP
{
    public partial class TopicTdc : System.Web.UI.UserControl
    {
        TopicBl topicBl = new TopicBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int? id = UrlHelper.GetQueryStringID();
                Topic topic = null;
                if (id.HasValue)
                {
                    topic = topicBl.GetTopicByID(id.Value);
                }
                if (topic == null) ErrorHelper.RaiseNoValueError();

                ViewState["id"] = topic.ID;

                this.TxtTitle.Text = topic.Title;
                this.TxtNo.Text = topic.No;
                this.TxtType.Text = TopicTypeDicMgr.GetByCode(topic.Type).Title;
                this.TxtTdc.Text = topic.Tdc;
                if (topic.PptFileID.HasValue)
                {
                    this.AfuFile.SetFile(topic.PptFileID.Value, new AttachmentBl().GetAttachmentByID(topic.PptFileID.Value).Title,
                          AttachmentUrl.GetFileUrl(topic.PptFileID.Value, true));
                }
                else
                {
                    this.AfuFile.SetFile(null, null, null);
                }
                this.TxtStart.Text = topic.StartTime.HasValue ? topic.StartTime.Value.ToString("yyyy-MM-dd") : "";
                this.TxtDeadline.Text = topic.DeadlineTime.HasValue ? topic.DeadlineTime.Value.ToString("yyyy-MM-dd") : "";
                this.TxtReview.Text = topic.ReviewTime.HasValue ? topic.ReviewTime.Value.ToString("yyyy-MM-dd") : "";
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (!this.AfuFile.FinalFileID.HasValue)
            {
                MessageHelper.Show("Please upload PPT file!", this.Page);
                return;
            }

            try
            {
                this.topicBl.UploadKickOffMaterials((int)ViewState["id"], this.AfuFile.FinalFileID.Value);

                MessageHelper.Show("Success!", this.Page);

                this.BtnSave.Enabled = false;
            }
            catch (ApplicationException aex)
            {
                MessageHelper.Show(aex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.Handle(ex);
            }
        }

    }
}